package com.android.aqq;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.tsinghua.lumaqq.qq.QQClient;

public class QQDialogActivity extends Activity {
	private Intent intent;
	private NotificationManager mNotificationManager;
	private QQService mBoundService;
	public static QQClient client = null;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.dialog);
        
        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                android.R.drawable.ic_dialog_alert);
        setTitle("添加好友");
        TextView tv = (TextView)findViewById(R.id.text);
		
		intent = getIntent();
		String message = intent.getStringExtra("MESSAGE");
		final int qq = intent.getIntExtra("QQ", 0);
		
		tv.setText(qq + "请求加你好友：" + message);
		
		//绑定QQService，设置service的controlHandler
		Intent intent = new Intent(QQDialogActivity.this, QQService.class);
		startService(intent);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
		
//		Button approveAddButton = (Button) findViewById(R.id.approve_add_button);
//		approveAddButton.setOnClickListener(new OnClickListener() {
//			public void onClick(View arg0) {
//				//同意加好友
//				client.user_ApproveAdd(qq);
//				finish();
//			}
//		});
		
		Button approveButton = (Button) findViewById(R.id.approve_button);
		approveButton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				//同意加好友
				client.user_ApproveAdd(qq);
				finish();
			}
		});
		
		Button rejectButton = (Button) findViewById(R.id.reject_button);
		rejectButton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				client.user_RejectAdd(qq, "");
				finish();
			}
		});
		
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		mNotificationManager.cancel(qq);
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((QQService.LocalBinder)service).getService();
            client = mBoundService.getClient();
    		//取消绑定QQService
    		unbindService(mConnection);
        }

        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };
}
